import com.google.common.cache.*;
import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class Test01 {

    private static Cache<String, String> tmp = CacheBuilder.newBuilder()
            .maximumSize(3)
            .build();

    @Test
    public void test01() {
        Cache<String, String> cache = CacheBuilder.newBuilder().
                maximumSize(2).expireAfterWrite(3, TimeUnit.SECONDS).build();
        cache.put("k1", "v1");
        cache.put("k2", "v2");
        //cache.put("k3", "v3");
        Integer time = 1;
        for (; ; ) {
            System.out.println("第" + (time++) + "次获取到的数据为：" + cache.getIfPresent("k1"));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test02() {
        Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(2).build();
        var total = 3;
        for (var i = 0; i < total; i++) {
            cache.put("k" + i, "v" + i);
        }
        for (var i = 0; i < total; i++) {
            System.out.println(cache.getIfPresent("k" + i));
        }
    }

    @Test
    public void test03() {
        Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(2).weakValues().weakKeys().build();
        String s = new String("before");
        cache.put("k1", s);
        s = new String("after");
        System.gc();
        Assert.assertEquals(null, cache.getIfPresent("k1"));
    }

    @Test
    public void test04() {
        Cache<String, String> cache = CacheBuilder.newBuilder().build();
        cache.put("k1", "v1");
        cache.put("k2", "v2");
        cache.put("k3", "v3");
        cache.invalidate("k2");
        System.out.println(cache.getIfPresent("k1"));
        System.out.println(cache.getIfPresent("k2"));
        System.out.println(cache.getIfPresent("k3"));
        List<String> removeList = new ArrayList<String>();
        removeList.add("k1");
        cache.invalidateAll(removeList);
        System.out.println(cache.getIfPresent("k1"));
        System.out.println(cache.getIfPresent("k2"));
        System.out.println(cache.getIfPresent("k3"));
    }

    @Test
    public void test05() {
        RemovalListener<String, String> listener = new RemovalListener<String, String>() {
            public void onRemoval(RemovalNotification<String, String> removalNotification) {
                System.out.println("[" + removalNotification.getKey() + ":" + removalNotification.getValue() + "] is removed.");
            }
        };
        Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(2).removalListener(listener).build();
        cache.put("k1", "v1");
        cache.put("k2", "v2");
        cache.put("k3", "v3");
        cache.put("k4", "v4");
        cache.put("k5", "v5");
    }

    @Test
    public void test06() {

    }

    public static void main(String[] args) {
        final CacheLoader<String, String> cacheLoader = new CacheLoader<>() {
            @Override
            public String load(String s) throws Exception {
                Thread.sleep(1000);
                System.out.println(s + "is loaded from a cacheLoader");
                return s;
            }
        };
        LoadingCache<String, String> loadingCache = CacheBuilder.newBuilder().maximumSize(3).build(cacheLoader);
        try {
            loadingCache.get("k1");
            loadingCache.get("k2");
            loadingCache.get("k3");
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    }

